<template>
  <!-- 添加或修改项目开票信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="80%">
    <el-form :model="form" :rules="rules" label-width="120px" ref="form">
      <el-tabs class="detail-tab" type="card" v-model="activeName">
        <el-tab-pane label="开票明细" name="first">
          <el-descriptions class="margin-top " :contentStyle="CS" :label-style="LS" title="" :column="2" size="medium"
                           border>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-c-scale-to-original"></i>
                客户名称
              </template>
              {{ applyForm.customerName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-time"></i>
                税号
              </template>
              {{ applyForm.taxNo }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-money"></i>
                开票金额
              </template>
              {{ applyForm.amount }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-money"></i>
                申请时间
              </template>
              {{ parseTime(applyForm.invoiceApplyDate, '{y}-{m}-{d} {h}:{i}:{s}') }}
            </el-descriptions-item>

            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-c-scale-to-original"></i>
                订单编号
              </template>
              {{ applyForm.projectNo }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-bank-card"></i>
                开票明细
              </template>
              <file-upload-show :value="applyForm.files"></file-upload-show>
            </el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
      </el-tabs>

      <el-tabs class="detail-tab" type="card" v-model="activeName">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="发票编号" prop="taxNo">
                <el-input placeholder="请输入发票编号" v-model="form.taxNo"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票金额" prop="invoiceMoney">
                <el-input-number :max="form.invoiceMoney" :precision="2" placeholder="请输入开票金额" disabled
                                 v-model="form.invoiceMoney"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票日期" prop="invoiceDate">
                <el-date-picker clearable
                                placeholder="请选择开票日期"
                                type="date"
                                v-model="form.invoiceDate"
                                value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发票文件" prop="image">
                <file-upload v-model="form.image" @getInvoiceInfo="getInvoiceInfo"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注信息" prop="infomation">
                <el-input placeholder="请输入内容" type="textarea" v-model="form.infomation"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button @click="submitForm" type="primary">开 票</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
<!--    <goods-invoice-info-list-in-form ref="goodsInvoiceInfoListInForm" :mainId="applyForm.mainId"-->
<!--                                     @delete="handleDeleteFundBack"></goods-invoice-info-list-in-form>-->
  </el-dialog>
</template>

<script>
import {addAction, getById, isEmpty, updateAction} from '@/api/manage'
import GoodsInvoiceInfoListInForm from "./GoodsInvoiceInfoListInForm";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";

export default {
  name: "InvoiceInfoForm",
  dicts: [],
  components: {
    FileUploadShow,
    GoodsInvoiceInfoListInForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发票申请数据
      applyForm: {},
      // 表单参数
      form: {},
      CS: {
        'text-align': 'center',  //文本居中
        'min-width': '250px',   //最小宽度
        'word-break': 'break-all'  //过长时自动换行
      },
      LS: {
        'color': '#000',
        'text-align': 'center',
        'font-weight': '600',
        'height': '40px',
        'min-width': '110px',
        'word-break': 'keep-all'
      },
      // 表单校验
      rules: {
        invoiceMoney: [
          {required: true, message: "发票金额不能为空", trigger: "blur"}
        ],
      },
      url: {
        getById: '/goodsInvoiceInfo/goodsInvoiceInfo/',
        add: '/goodsInvoiceInfo/goodsInvoiceInfo/add',
        edit: '/goodsInvoiceInfo/goodsInvoiceInfo/edit',
      },
    };
  },
  created() {

  },
  methods: {
    handleDeleteFundBack() {//开票记录删除时调用父页面刷新列表
      this.$emit('ok');
    },
    addInvoiceInfo(data) {
      let that = this;
      that.reset();
      that.open = true;
      that.applyForm.mainId = data.id;
      that.applyForm.projectId = data.projectId;
      that.applyForm.projectName = data.projectName;
      that.applyForm.projectNo = data.projectNo;
      that.applyForm.invoiceMoney = data.remaining;
      that.applyForm.invoiceApplyDate = data.invoiceApplyDate;
      that.applyForm.taxNo = data.taxNo;
      that.applyForm.customerName = data.customerName;
      that.applyForm.amount = data.amount;
      that.applyForm.files = data.files;
      that.form.invoiceMoney = data.remaining;
      that.title = "添加发票信息";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        invoiceMoney: null,
        invoiceDate: new Date(),
        infomation: null,
        projectId: null,
        projectName: null,
        preNo: null,
        taxNo: null,
        files: null,
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      let that = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          that.form.backMoney = 0;
          that.form.remainMoney = that.form.invoiceMoney;
          that.form.status = 0;
          that.form.mainId = that.applyForm.mainId;
          if (this.form.id != null) {
            /*updateAction(this.url.edit, this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$emit('ok');
            });*/
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    getInvoiceInfo(info) {
      console.log(info);
      if (!isEmpty(this.form.taxNo)) {
        this.form.taxNo += ",";
      } else {
        this.form.taxNo = "";
      }
      this.form.taxNo += info.number;
      this.form.invoiceMoney += info.totalAmount;
    }


  }
};
</script>
